/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gl.tp1;

/**
 *
 * @author wadi3
 */
import java.sql.*;
import java.util.Vector;

public class ClientBase {

    public Utilisateur loadUser(String login, String pass) {
        Connection conn = UtilitaireConnection.getConnection();
        Utilisateur u = null;
        try {
            Statement stmt = conn.createStatement();
            String query   = "select * from ROOT.\"UTILISATEURS\" where login=" + login + "and pass=" + pass + "\"";
            ResultSet rsUser = stmt.executeQuery(query);
            if (rsUser.next()) {
                u = new Utilisateur();
                u.setIdUser(new Integer(rsUser.getInt("ID_USER")));
                u.setLogin(rsUser.getString("LOGIN"));
                u.setPass(rsUser.getString("PASS"));

                u.setEmail(rsUser.getString("EMAIL"));
                u.setVille(rsUser.getString("VILLE"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return u;
    }

    public void addUser(String l, String p, String e, String v) {
        Connection conn = UtilitaireConnection.getConnection();
        Utilisateur u = null;
        try {
            Statement s = conn.createStatement();
            String query = "insert into ROOT.\"UTILISATEURS\"(LOGIN,PASS,EMAIL,VILLE) values(" + l + "," + p + "," + e + "," + v + ")";
            s.executeUpdate(query);
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
    }

    public Vector<Utilisateur> selectAll() {
        Connection conn = UtilitaireConnection.getConnection();
        Vector users = new Vector();
        Utilisateur u = null;
        try {
            Statement stmt = conn.createStatement();
            String query = "select * from ROOT.\"UTILISATEURS\"";
            ResultSet rsUser = stmt.executeQuery(query);
            while (rsUser.next()) {
                u = new Utilisateur();
                u.setIdUser(new Integer(rsUser.getInt("ID_USER")));
                u.setLogin(rsUser.getString("LOGIN"));
                u.setPass(rsUser.getString("PASS"));
                u.setEmail(rsUser.getString("EMAIL"));
                u.setVille(rsUser.getString("VILLE"));
                users.add(u);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }

    public static void main(String[] args) {
//Instantiation de la classe

        ClientBase cbs = new ClientBase();
//ajout d'un utilisateur
        cbs.addUser("'a'", "'b'", "'c'", "'d'");
//affichage de tous les utilisateurs
        Vector<Utilisateur> v = cbs.selectAll();
        for (int i = 0; i < v.size(); i++) {
            System.out.println(v.elementAt(i).getEmail());
        }
    }
}